/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2010 year
 *    Author: Michael Khromov aka MikeDM (Mike D'Morto)
 *    E-Mail: mikedm@list.ru
 */


#include "mlog.h"

mlog::mlog(QObject *parent) :
    QObject(parent)
{
    this->logstate=false;

}
bool mlog::setlogfile(const QString & fname){
    // open file for log
    this->file.setFileName(fname);
    if(!this->file.open(QFile::WriteOnly | QFile::Append)){
        this->slot_print((tr("ERROR open log file ")+fname));
        return false;
    }else{
        this->logstate=true;
        return true;
    }
    return false;
}

void mlog::slot_print(const QString & text){
    if(!this->logstate){
        // file not open, print data to stdout
        printf(text.toAscii());
    }else{
        // file good =) put to him
        QTextStream gg;
        gg.setDevice(&this->file);
        QString ll(text);
        ll.remove(QChar('\n'), Qt::CaseInsensitive);
#if defined(Q_OS_LINUX)
        gg<<ll<<"\n";
#elif defined(Q_OS_WIN)
        gg<<ll<<"\r\n";
#endif

        if(gg.status() != QTextStream::Ok){
            // error write to logfile
            this->logstate=false;
            this->file.close();
            this->slot_print(tr("ERROR write to tester log file\n"));
        }

    }
}
